package com.group3.customer.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group3.customer.service.IUserService;
import com.group3.customer.service.impl.UserServiceImpl;
import com.group3.customer.util.SenderUtil;
import com.group3.entity.Customer;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       private IUserService service=new UserServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		boolean flag=register(request, response);
Map<String, Object> map=new HashMap<String, Object>();
		if (flag) {
			map.put("status", "200");
			
		}else {
			map.put("status", "401");
		}
		SenderUtil.send(map, response);
	}
	private boolean register(HttpServletRequest request, HttpServletResponse response){
		boolean flag=false;
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String id=UUID.randomUUID().toString();
		Customer user=new Customer(id,username,password);
		Customer result=service.register(user);
		if (result!=null) {
			flag=true;
		}
		return flag;
		
	}

}
